//
//  WJZDYButton.m
//  KMedicine
//
//  Created by wangjun on 16/7/7.
//  Copyright © 2016年 caomu. All rights reserved.
//

#import "WJZDYButton.h"


#define k_SureYesString @"已确诊"
#define k_SureNoString @"症状疑似"
#define k_AcceptYesString @"接受过"
#define k_AcceptNoString @"未接受过"

@interface WJZDYButton()
@property(nonatomic,weak)UITextField *tf_separated;
@property(nonatomic,weak)UIView *lineView;
@property(nonatomic,weak)UIImageView *accossoryImageView;
@property(nonatomic,weak)UIButton *SureButton;
@property(nonatomic,weak)UIButton *acceptButton;
@property(nonatomic,assign)BOOL statusAccept;
@property(nonatomic,assign)BOOL statusSure;
@end

@implementation WJZDYButton


- (void)setString:(NSString *)string{
    
    _string = string;
    
    _tf_separated.text = string;
    WJLog(@"%@",string);
}



- (instancetype)initWithPlaceHolder:(NSString *)placeHolder {
    
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        //tf_separated
        UITextField *tf_separated = [[UITextField alloc]init];
        self.tf_separated = tf_separated;
        tf_separated.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 5)];
        tf_separated.leftViewMode = UITextFieldViewModeAlways;
        tf_separated.enabled = NO;
        tf_separated.backgroundColor = [UIColor whiteColor];
        tf_separated.placeholder = placeHolder;
        
        [self addSubview:tf_separated];
        
        if ([placeHolder isEqualToString:@"请选择疾病细分"]||[placeHolder isEqualToString:@"请选择并发症(可多选)"]) {
            
            //----灰色分割线
            UIView *lineView = [[UIView alloc]init];
            self.lineView = lineView;
            lineView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
            [self addSubview:lineView];
            
            //右侧箭头指示器
            UIImageView *accossoryImageView = [[UIImageView alloc]init];
            self.accossoryImageView = accossoryImageView;
            accossoryImageView.image = [UIImage imageNamed:@"illness_button_input_blue.png"];
            [self addSubview:accossoryImageView];
            [self setConstraintTypeOne];
            
        }else if ([placeHolder isEqualToString:@"是否确诊"]){
            
            [self buttonAddSureComponents:k_SureYesString withNoComponents:k_SureNoString];
            [self setConstraintTypeTwo];
            
        }else if ([placeHolder isEqualToString:@"是否接受治疗"]){
            
            [self buttonAddSureComponents:k_AcceptYesString withNoComponents:k_AcceptNoString];
            [self setConstraintTypeTwo];
            
        }else if ([placeHolder isEqualToString:@"请选择治疗方式"]){
            //----灰色分割线
            UIView *lineView = [[UIView alloc]init];
            self.lineView = lineView;
            lineView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
            [self addSubview:lineView];
            
            //右侧箭头指示器
            UIImageView *accossoryImageView = [[UIImageView alloc]init];
            self.accossoryImageView = accossoryImageView;
            accossoryImageView.image = [UIImage imageNamed:@"illness_button_input_blue.png"];
            [self addSubview:accossoryImageView];
            [self setConstraintTypeOne];
            
        }
        
    }
    return self;
}

- (void)buttonAddSureComponents:(NSString *)yesStr withNoComponents:(NSString *)noStr{
    
    //确诊SureButton
    UIButton *SureButton = [[UIButton alloc]init];
    self.SureButton = SureButton;
    SureButton.selected = YES;
    [SureButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [SureButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    [SureButton setTitle:yesStr forState:UIControlStateNormal];
    NSDictionary *dictSure = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blackColor]};
    NSAttributedString *attrSure = [[NSAttributedString alloc]initWithString:SureButton.titleLabel.text attributes:dictSure];
    [SureButton setAttributedTitle:attrSure forState:UIControlStateNormal];
    [self addSubview:SureButton];
    [SureButton addTarget:self action:@selector(upButton:) forControlEvents:UIControlEventTouchUpInside];
    //接受acceptButton
    UIButton *acceptButton = [[UIButton alloc]init];
    self.acceptButton = acceptButton;
    
    acceptButton.titleLabel.textColor = [UIColor blackColor];
    [acceptButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [acceptButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    [acceptButton setTitle:noStr forState:UIControlStateNormal];
    NSDictionary *dictAccept = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blackColor]};
    NSAttributedString *attrAcc = [[NSAttributedString alloc]initWithString:acceptButton.titleLabel.text attributes:dictAccept];
    [acceptButton setAttributedTitle:attrAcc forState:UIControlStateNormal];
    [self addSubview:acceptButton];
    [acceptButton addTarget:self action:@selector(upButton:) forControlEvents:UIControlEventTouchUpInside];
    
}

//按钮监听
- (void)upButton:(WJZDYButton *)sender {
    
    // 如果 当前被点击为选中状态  直接 return
    if (sender.selected == YES) {
        
        return;
    }
    
    if ([sender.currentTitle isEqualToString:@"接受过"] || [sender.currentTitle isEqualToString:@"未接受过"]) {
        
        if (self.buttonBlock) {
            
            self.buttonBlock(self.SureButton.selected);
        }
    }
    
    //赋当前值
    self.tf_separated.text = sender.currentTitle;
    
    // 全部制空
    self.acceptButton.selected = NO;
    self.SureButton.selected = NO;
    
    // 再取反
    sender.selected = !sender.isSelected;
    
}

#pragma mark -
#pragma mark -设置约束
- (void)setConstraintTypeOne{
    
        //textField
    [self.tf_separated makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self).offset(0);
        make.leading.equalTo(self).offset(0);
        make.bottom.bottom.equalTo(self).offset(0);
        make.trailing.mas_equalTo(self.lineView.leading).offset(-10);
    }];
    
    //右侧箭头指示器
    [self.accossoryImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(6);
        make.top.mas_equalTo(15);
        make.bottom.mas_equalTo(-15);
        make.trailing.offset(-12);
        
    }];
    
    //----灰色分割线
    [self.lineView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.offset(5);
        make.trailing.mas_equalTo(self.accossoryImageView.mas_leading).offset(-12);
        make.bottom.offset(-10);
        make.width.equalTo(3);
    }];
    
}

- (void)setConstraintTypeTwo{
    
    //textField
    [self.tf_separated makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self).offset(0);
        make.leading.equalTo(self).offset(0);
        make.bottom.bottom.equalTo(self).offset(0);
        make.trailing.mas_equalTo(self.mas_trailing).offset(-5);
    }];
    
    //确诊SureButton
    [self.SureButton makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.mas_equalTo(self.acceptButton.mas_leading).offset(0);
        make.centerY.mas_equalTo(self.acceptButton.mas_centerY).offset(0);
        make.height.mas_equalTo(self.acceptButton.mas_height);
        make.width.mas_equalTo(90);
    }];
    
    //接受acceptButton
    [self.acceptButton makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.mas_equalTo(self).offset(-10);
        make.centerY.mas_equalTo(self.centerY).offset(0);
        make.width.equalTo(90);
        make.height.mas_equalTo(20);
    }];
}

- (void)dealloc{
    
    WJLog(@"%@",self.acceptButton.currentTitle);
}

@end
